package com.example.demo.controller;


import com.example.demo.entity.Item;
import com.example.demo.mapper.ItemMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    ItemMapper mapper;

    @RequestMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "")String mohu, @RequestParam(defaultValue = "1")String current){
        PageHelper.startPage(Integer.valueOf(current), 4);
        List<Item> list = mapper.findAll(mohu);
        PageInfo p = new PageInfo<>(list);
        model.addAttribute("mohu",mohu);
        model.addAttribute("p",p);
        return "list";
    }


    @RequestMapping("add")
    public String add(){
        return "add";
    }

    @RequestMapping("update")
    public String update(){
        return "update";
    }
   @RequestMapping("toAdd")
    @ResponseBody
   private boolean addItem(Item item){
        return mapper.insertSelective(item);
    }

    @RequestMapping("delete")
    @ResponseBody
    private boolean delete(String id){
        return mapper.deleteByPrimaryKey(id);
    }

    @RequestMapping("updateDate")
    @ResponseBody
    private Item updateDate(Long id){
       Item i= mapper.updateDate(id);
       return i;
    }

    @RequestMapping("updateItem")
    @ResponseBody
    private boolean updateItem(Item item){
        return mapper.updateByPrimaryKeySelective(item);
    }
}


